import requests
import re
import pymysql

# 连接到 MySQL 数据库
connection = pymysql.connect(
    host='localhost',
    user='root',
    password='123456',
    database='mydb1',
    charset='utf8mb4',
    cursorclass=pymysql.cursors.DictCursor
)

try:

    session = requests.Session()
    data = {
        "csrf_token": "JuUZjLcpehvHAlFEIzXgPKnOxCsQWfytMDkYiRSNGrVBomTadbqw",
        "username": "123",
        "password": "123"
    }

    login_url = "https://quotes.toscrape.com/login"

    res = session.post(login_url, data=data)
    print(res.cookies)

    all_data = []

    url = "https://quotes.toscrape.com/"
    while True:
        response = session.get(url)
        html_content = response.text

        # 定义正则表达式来匹配名言、作者和标签
        quote_pattern = re.compile(r'<span class="text" itemprop="text">“(.*?)”</span>')
        author_pattern = re.compile(r'<small class="author" itemprop="author">(.*?)</small>')
        tag_pattern = re.compile(r'<a class="tag" href="/tag/.*?">(.*?)</a>')

        # 查找所有名言
        quotes = quote_pattern.findall(html_content)
        authors = author_pattern.findall(html_content)
        tag_matches = tag_pattern.findall(html_content)

        quote_blocks = re.findall(r'<div class="quote".*?</div>', html_content, re.DOTALL)

        for block in quote_blocks:
            text_match = quote_pattern.search(block)
            author_match = author_pattern.search(block)
            tag_in_block = tag_pattern.findall(block)

            text = text_match.group(1) if text_match else ""
            author = author_match.group(1) if author_match else ""
            tags = ",".join(tag_in_block)

            all_data.append({
                'text': text,
                'author': author,
                'tags': tags
            })

        # 查找下一页的链接
        next_page_pattern = re.findall(r'<a href="(.*?)">Next', html_content)
        if next_page_pattern:
            url = "https://quotes.toscrape.com/" + next_page_pattern[0]
        else:
            break

    # 将数据插入到数据库中
    with connection.cursor() as cursor:
        insert_query = "INSERT INTO quotes (text, author, tags) VALUES (%s, %s, %s)"
        for data in all_data:
            cursor.execute(insert_query, (data['text'], data['author'], data['tags']))
    connection.commit()

    for data in all_data:
        print(data)

except Exception as e:
    print(f"发生错误: {e}")
finally:
    connection.close()
